/*
 * @Description:
 * @Version: 2.0
 * @Autor: kafei
 * @Date: 2021-08-20 15:32:58
 * @LastEditors: kafei
 * @LastEditTime: 2021-11-18 17:48:42
 */
const ValidatorMap = {
  emailValidator: {
    pattern: '^([a-zA-Z0-9]+[_|\\_|\\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\\_|\\.]?)*[a-zA-Z0-9]+\\.[a-zA-Z]{2,3}$',
    message: '邮箱校验不通过'
  },
  urlValidator: {
    pattern: '(https?|ftp|file):\\/\\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]',
    message: '网址校验不通过'
  },
  numberValidator: {
    pattern: '^\\d+$',
    message: '数字校验不通过'
  },
  telephoneValidator: {
    pattern: '^(\\+\\d+(-|\\s)?)?(\\d+(-|\\s)?)*\\d+$',
    message: '电话验证格式不正确'
  },
  idCardValidator: {
    pattern: '^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$',
    message: '身份证格式不正确'
  },
  twoNumberValidator: {
    pattern: '^(0|[1-9]\\d*)(.\\d{1,2})?$',
    message: '数字校验不通过'
  },
  thirtyNumberValidator: {
    pattern: '^\\d{0,13}(\\.?\\d{0,2})$',
    message: '最多13位小数'
  }
}
/**
   * @description:示例 如果检验不通过则会返回message,检验通过则返回undefined,业务可通过是否有返回在进行业务操作
   * @param {value:需要校验的值,Array:需要校验的list}
   * @return {String || undefined}
   * @author: 黄康辉
   */
//  import { validataFun } from '@mod/crm/utils/validator'
//  const a = [{ pattern: 'numberValidator',message:"自定义的提醒" },{pattern:"emailValidator",message:"自定义的提醒"}]
//  const msg = validataFun('1221', a)
//  if(msg){
//    this.$message.error(msg)
//  } else {
//    dosomething
// }
//  console.log(msg, 'zzz')

// const pattern = [
//   { pattern: 'required', message: '姓名字段不能为空' },
//   { pattern: 'max', value: 3, message: '最大值为3' },
//   { pattern: 'min', value: 2, message: '最小值为2' }
// ]
// console.log(validataFun('1', pattern))
const maxAndMin = ['max', 'min']
class Validator {
  constructor () {
    this.caches = []
  }

  handleMaxAndMin (val, pattern, length, msg) {
    const len = length * 1
    const whereBig = pattern === 'max' ? val.length <= len : val.length >= len
    return whereBig ? '' : msg
  }

  regText (val, reset) {
    const { pattern, message: msg, value } = reset
    if (pattern === 'required') {
      if (!val || val.length === 0) {
        return msg
      }
    } else if (maxAndMin.includes(pattern)) {
      return this.handleMaxAndMin(val, pattern, value, msg)
    } else {
      let patternStr
      if (ValidatorMap[pattern]) {
        patternStr = ValidatorMap[pattern].pattern
      } else {
        patternStr = pattern
      }
      const reg = new RegExp(patternStr)
      if (!reg.test(val)) {
        let targetMsg = ''
        if (!msg && ValidatorMap[pattern]) {
          targetMsg = ValidatorMap[pattern].message
        } else if (!msg && !ValidatorMap[pattern]) {
          targetMsg = '校验失败'
        } else {
          targetMsg = msg
        }
        return targetMsg
      } else {
        return ''
      }
    }
    return ''
  }

  add (val, reg) {
    for (let i = 0; i < reg.length; i++) {
      this.caches.push(() => {
        return this.regText(val, reg[i])
      })
    }
  }

  start () {
    for (let i = 0; i < this.caches.length; i++) {
      const msg = this.caches[i]()
      if (msg) {
        return msg
      }
    }
  }
}

const validataFun = (val, reg) => {
  if (!reg || reg.length === 0) {
    return ''
  }
  var validator = new Validator()
  validator.add(val, reg)
  const msg = validator.start()
  return msg
}
/**
 * @description: 批量处理校验规则
 * @param {*}
 * @return {*}
 * @author: kafei
 */
const handleValidator = (target) => {
  let msg = ''
  for (let i = 0; i < target.length; i++) {
    const value = target[i].value
    const pattern = target[i].pattern
    msg = validataFun(value, pattern)
    if (msg) {
      return msg
    }
  }
  return msg
}

module.exports = {
  validataFun,
  ValidatorMap,
  handleValidator
}
